<?php
/**
 * 用户信息
 * @copyright (c) Seer All Rights Reserved
 */

class User_Info_Model {

	private $db;

	function __construct() {
		$this->db = Database::getInstance();
	}


	/**
	 * 得到用户信息主账号信息
	 * 可判断是否注册
	 * @param string $family_id
	 */
	function getViewUserInfo($open_id) {
		$sql = "SELECT * FROM " . DB_PREFIX . "view_user_info WHERE openid = '$open_id'";
		$res = $this->db->query($sql);

		$userSimpleInfos = array();
		while ($row = $this->db->fetch_array($res)) {
			$userSimpleInfos[] = $row;
		}
		return $userSimpleInfos;
	}

	/**
	 * 发优惠卡操作
	 *
	 * @param int $cardID
	 * @param string $open_id
	 */
	function addUserInfo($userData) {
		$time=date("Y-m-d H:i:s");
		$openid=$userData["openid"];
		$family_id=$userData["family_id"];
		$tel=$userData["tel"];
		$sex=$userData["sex"];
		$nickname=$userData["nickname"];
		$birthday=$userData["birthday"];
		$school_name=$userData["school"];
		$identify_label	=$userData["identify_label"];
		$head_img_url =$userData["head_img_url"];
		$is_main_account =$userData["is_main_account"];

		$sql="insert into ".DB_PREFIX."user_info (openid,family_id,tel,sex,nickname,birthday,school_name,identify_label,head_img_url,is_main_account,create_time,modify_time) "
			."values('$openid','$family_id','$tel','$sex','$nickname','$birthday','$school_name','$identify_label','$head_img_url','$is_main_account','$time','$time')";
		$this->db->query($sql);
		return mysql_insert_id();
	}

	function updateUserInfo($userData) {
		$uid = $userData["uid"];
		$time=date("Y-m-d H:i:s");
		$Item = array();
		foreach ($userData as $key => $data) {
			$Item[] = "$key='$data'";
		}
		$upStr = implode(',', $Item);
		$this->db->query("update ".DB_PREFIX."user_info set $upStr,modify_time='$time' where uid='$uid'");
	}

	/**
	 * 得到用户信息
	 *
	 * @param string $open_id
	 */
	function getUserInfo($open_id) {
		$sql = "SELECT * FROM " . DB_PREFIX . "user_info WHERE openid = '$open_id'";
		$res = $this->db->once_fetch_array($sql);
		$user = array();
		if($res) {
			$user = $res;
		}
		return $user;
	}

	/**
	 * 得到用户信息
	 *
	 * @param string $open_id
	 */
	function getUserInfoByFamilyId($familyId) {
		$sql = "SELECT count(*) as count FROM " . DB_PREFIX . "user_info WHERE family_id = '$familyId'";
		$res = $this->db->once_fetch_array($sql);
		return $res["count"];
	}

	/**
	 * 得到用户信息
	 *
	 * @param string $open_id
	 */
	function getUserInfoForView($open_id) {
		$sql = "SELECT * FROM " . DB_PREFIX . "view_user_info WHERE openid = '$open_id'";
		$res = $this->db->once_fetch_array($sql);
		$user = array();
		if($res) {
			$user = $res;
		}
		return $user;
	}

	/**
	 * 得到用户信息
	 *
	 * @param string $family_id
	 */
	function getSimpleUserInfo($family_id) {
		$sql = "SELECT nickname,head_img_url,identify_label FROM " . DB_PREFIX . "user_info WHERE family_id = '$family_id'";
		$res = $this->db->query($sql);
		$userSimpleInfos = array();
		while ($row = $this->db->fetch_array($res)) {
			$userSimpleInfos[] = $row;
		}
		return $userSimpleInfos;
	}

	/**
	 * 后台获取用户列表
	 *
	 * @param string $condition
	 * @param int $page
	 * @param string $cardName
	 * @return array
	 */
	function getUsersForAdmin($condition = '', $page = 1, $cardName = '') {
		$perpage_num = Option::get('admin_perpage_num');
		$start_limit = !empty($page) ? ($page - 1) * $perpage_num : 0;
		$limit = "LIMIT $start_limit, " . $perpage_num;
		$sql = "SELECT * FROM " . DB_PREFIX . "card WHERE card_name like '%$cardName%' order by buildtime desc $limit";
		$res = $this->db->query($sql);
		$cards = array();
		while ($row = $this->db->fetch_array($res)) {
			$cards[] = $row;
		}
		return $cards;
	}

	/**
	 * 后台获取用户数量
	 *
	 * @param string $cardName
	 * @return array
	 */
	function getUsersNum($cardName = '') {
		$andQuery = '1=1';
		$andQuery .= $cardName ? " and c.card_name like '%$cardName%'" : '';
		$sql = "SELECT count(*) FROM ".DB_PREFIX."card as c where $andQuery";
		$res = $this->db->once_fetch_array($sql);
		$comNum = $res['count(*)'];
		return $comNum;
	}
}
